/* 
 *  <copyright file="TextureComponent.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace SobrietyEngine.Components.Graphic
{
    public class Sprite2DCmp:Base2DGraphic, SobrietyEngine.Components.IComponent
    {
        //TODO: graphic stuff
        protected String TextureName;

        protected Texture2D spriteTexture;
        public Texture2D SpriteTexture { get { return spriteTexture; } set { spriteTexture = value; } }

        protected IPosition position;
        protected IOrientation rotation;
        protected IScale scale;

        protected Vector2 SpriteHalfSize;
        protected Vector2 rotationOrigin = new Vector2(0, 0);

        protected float rot = 0.0f;
        protected float scl = 1.0f;
        protected SpriteEffects spriteEffects = SpriteEffects.None;
        Vector2 pos = new Vector2();

        public Sprite2DCmp():base()
        {
        }

        public Sprite2DCmp(String _textureName)
            : base()
        {
            TextureName = _textureName;
        }

         override public Boolean InitComponent()
        {
            spriteTexture = Actor.GameServices().TextureGameService[TextureName];
            SpriteHalfSize.X = spriteTexture.Width / 2;
            SpriteHalfSize.Y = spriteTexture.Height / 2;

            position = Actor.Get<IPosition>();
            if(position==null)
                throw new Exception("StaticTexture2D requires a position component");

            rotation = Actor.Get<IOrientation>();
            scale = Actor.Get<IScale>();

            if (spriteTexture.Width > spriteTexture.Height)
                boundingRadius = spriteTexture.Width;
            else
                boundingRadius = spriteTexture.Height;

            return true;
        }

        public override void UpdateGraphic(GameTime time)
        {
            if (rotation != null)
            {
                rot = rotation.GetRadiansXY();
                Vector3 ro = rotation.RotationOrigin;
                rotationOrigin = new Vector2(ro.X, ro.Y);
            }

            if (scale != null)
                scl = (float)scale.Scale.X;

            spriteEffects= SpriteEffects.None;
            if (flipImageHorz)
                spriteEffects |= SpriteEffects.FlipHorizontally;
            if (flipImageVert)
                spriteEffects |= SpriteEffects.FlipVertically;

            pos = position.PositionV2 - rotationOrigin;

        }
        override public void Draw(SpriteBatch spriteBatch)
        {
                spriteBatch.Draw(
                    spriteTexture, 
                    pos,
                    new Rectangle(0, 0, spriteTexture.Width, spriteTexture.Height),
                    Color,
                    rot,
                    SpriteHalfSize - rotationOrigin, //This is the center lookAt of the Orientation
                    scl, //This is the scaling
                    spriteEffects,
                    layer);    //this is the layer            
            }
    }
}